﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils;
using RegistrationController;

namespace HPMDesktop.Patient
{
    public partial class frmRegistration : Form
    {
        private bool _oldPatient;
        private Hashtable _consultantInfo;
        private string _registrationFees = "0";
        
        public frmRegistration()
        {
            InitializeComponent();
            _oldPatient = false;
        }

        public frmRegistration(String patientID)
        {
            InitializeComponent();
            txtPID.Text = patientID;
            _oldPatient = true;
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmRegistration_Load(object sender, EventArgs e)
        {
            txtRegFees.Text = "0";
            txtConsFees.Text = "0";
            txtDeposit.Text = "0";
            txtTotal.Text = "0";
            chkPatientType.SelectedIndex = 1;
            if (_oldPatient && !txtPID.Text.Equals(""))
            {
                PatientInfo objData = null;
                PatientRegistration_DataLoader objDataLoader = new PatientRegistration_DataLoader();

                try
                {
                    objData = objDataLoader.Get_PATIENT_DATA(txtPID.Text);

                    if (objData.PatientID == null ||
                        !objData.PatientID.Equals(txtPID.Text))
                    {
                        /* Patient Info could not be found in DB. Register as new. */
                        MessageBox.Show("Patient Record for ID " + txtPID.Text + " does not exist. Register as new.");
                        txtPID.Text = "";
                        _oldPatient = false;
                    }
                    else
                    {
                        txtFName.Text = Convert.ToString(objData.FName);
                        txtMName.Text = Convert.ToString(objData.MName);
                        txtLName.Text = Convert.ToString(objData.LName);
                        txtFatherFName.Text = Convert.ToString(objData.FatherFName);
                        txtFatherMName.Text = Convert.ToString(objData.FatherMName);
                        txtFatherLName.Text = Convert.ToString(objData.FatherLName);
                        txtAge.Text = Convert.ToString(objData.Age);
                        txtStreet.Text = Convert.ToString(objData.Address1);
                        txtCity.Text = Convert.ToString(objData.City);
                        txtState.Text = Convert.ToString(objData.State);
                        txtPhone.Text = Convert.ToString(objData.MobilePhone);
                        if (objData.Sex.Equals("F"))
                        {
                            rdoFemale.Checked = true;
                            rdoMale.Checked = false;
                        }
                        else
                        {
                            rdoMale.Checked = true;
                            rdoFemale.Checked = false;
                        }
                    }
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    if (objData != null)
                    {
                        objData.Dispose();
                    }
                    objDataLoader.Dispose();
                }
            }

            /* Load consultants from db */
            ConsultantInfo_DataLoader objCIDataLoader = new ConsultantInfo_DataLoader();

            _consultantInfo = objCIDataLoader.Get_ConsultantFees_Data();

            foreach (String consultantName in _consultantInfo.Keys)
            {
                chkConsultant.Items.Add(consultantName);
            }

            objCIDataLoader.Dispose();

            /* Get Registration Fees from db */
            GlobalData_DataLoader objGDDataLoader = new GlobalData_DataLoader();
            GlobalData objGlobalData = objGDDataLoader.Get_Global_Data();
            if (objGlobalData != null &&
                objGlobalData.RegistrationFees != null)
            {
                _registrationFees = objGlobalData.RegistrationFees;
                if (chkPatientType.Text.Equals("In-Patient"))
                {
                    txtRegFees.Text = _registrationFees;
                    txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
                }
            }

            objGlobalData.Dispose();
            objGDDataLoader.Dispose();
        }

        private void cmdRegister_Click(object sender, EventArgs e)
        {
            bool result;
            PatientRegistration objData = new PatientRegistration();
            PatientRegistration_Mapper objMapper = new PatientRegistration_Mapper();

            try
            {
                objData.PatientInfo.PatientID = Convert.ToString(txtPID.Text);
                objData.PatientInfo.FName = Convert.ToString(txtFName.Text);
                objData.PatientInfo.MName = Convert.ToString(txtMName.Text);
                objData.PatientInfo.LName = Convert.ToString(txtLName.Text);
                objData.PatientInfo.FatherFName = Convert.ToString(txtFatherFName.Text);
                objData.PatientInfo.FatherMName = Convert.ToString(txtFatherMName.Text);
                objData.PatientInfo.FatherLName = Convert.ToString(txtFatherLName.Text);
                objData.PatientInfo.MobilePhone = Convert.ToString(txtPhone.Text);
                objData.PatientInfo.Address1 = Convert.ToString(txtStreet.Text);
                objData.PatientInfo.City = Convert.ToString(txtCity.Text);
                objData.PatientInfo.State = Convert.ToString(txtState.Text);
                objData.PatientInfo.Age = Convert.ToString(txtAge.Text);
                if (rdoFemale.Checked)
                {
                    objData.PatientInfo.Sex = "F";
                }
                else if (rdoMale.Checked)
                {
                    objData.PatientInfo.Sex = "M";
                }
                objData.Consultant = Convert.ToString(chkConsultant.Text);
                objData.PatientType = chkPatientType.Text.Substring(0, 1);
                objData.RegistrationFees = Convert.ToString(txtRegFees.Text);
                objData.ConsultantFees = Convert.ToString(txtConsFees.Text);
                objData.InitialDeposit = Convert.ToString(txtDeposit.Text);
                objData.PackageName = txtPkgName.Text;

                result = objMapper.Load_PATIENTREGISTRATION_DATA(objData);
                txtPID.Text = Convert.ToString(objData.PatientInfo.PatientID);
                txtCRN.Text = Convert.ToString(objData.CRNumber);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                objData.Dispose();
                objMapper.Dispose();

            }
            cmdRegister.Enabled = false;
            cmdOk.Enabled = true;
            chkPaid.Enabled = false;
            cmdSelectPkg.Enabled = false;
            cmdClearPkg.Enabled = false;
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkPaid_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPaid.Checked.Equals(true))
            {
                cmdRegister.Enabled = true;
            }
            else
            {
                cmdRegister.Enabled = false;
            }
        }

        private void txtRegFees_LostFocus(object sender, EventArgs e)
        {
            /* TODO : Getting Lost Focus events multiple times,
             * need to check this
             */
            if (txtRegFees.Text.Equals(""))
            {
                txtRegFees.Text = "0";
            }
            if (txtConsFees.Text.Equals(""))
            {
                txtConsFees.Text = "0";
            }
            if (txtDeposit.Text.Equals(""))
            {
                txtDeposit.Text = "0";
            }
            txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) + 
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
        }

        private void txtConsFees_LostFocus(object sender, EventArgs e)
        {
            /* TODO : Getting Lost Focus events multiple times,
             * need to check this
             */
            if (txtRegFees.Text.Equals(""))
            {
                txtRegFees.Text = "0";
            }
            if (txtConsFees.Text.Equals(""))
            {
                txtConsFees.Text = "0";
            }
            if (txtDeposit.Text.Equals(""))
            {
                txtDeposit.Text = "0";
            }
            txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
        }

        private void txtDeposit_LostFocus(object sender, EventArgs e)
        {
            /* TODO : Getting Lost Focus events multiple times,
             * need to check this
             */
            if (txtRegFees.Text.Equals(""))
            {
                txtRegFees.Text = "0";
            }
            if (txtConsFees.Text.Equals(""))
            {
                txtConsFees.Text = "0";
            }
            if (txtDeposit.Text.Equals(""))
            {
                txtDeposit.Text = "0";
            }
            txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
        }

        private void chkConsultant_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chkPatientType.Text.Equals("Out-Patient"))
            {
                string consultantName = chkConsultant.Text.ToString();
                txtConsFees.Text = _consultantInfo[consultantName].ToString();
                txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
            }
        }

        private void chkPatientType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (chkPatientType.Text.Equals("In-Patient"))
            {
                cmdSelectPkg.Enabled = true;
                cmdClearPkg.Enabled = true;
                txtRegFees.Text = _registrationFees;
                txtConsFees.Text = "0";
                txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
            }
            else if (chkPatientType.Text.Equals("Out-Patient"))
            {
                cmdSelectPkg.Enabled = false;
                cmdClearPkg.Enabled = false;
                txtRegFees.Text = "0";
                if (chkConsultant.Text.ToString() != "")
                {
                    txtConsFees.Text = _consultantInfo[chkConsultant.Text.ToString()].ToString();
                }
                txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtConsFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
            }
        }

        private void cmdSelectPkg_Click(object sender, EventArgs e)
        {
            frmSelectPackage frm = new frmSelectPackage(this, "frmRegistration");
            frm.Show();
        }

        private void cmdClearPkg_Click(object sender, EventArgs e)
        {
            txtPkgName.Text = "";
            txtPkgAmount.Text = "";
        }

        public void setPackageDetails(string packageName, string packageAmount)
        {
            txtPkgName.Text = packageName;
            txtPkgAmount.Text = packageAmount;
        }
    }
}
